function emit () {
  const all = new Map()
  return {
    all,
    on(key, handler) {
      if (!all.has(key)) {
        all.set(key, [])
      }
      all.get(key).push(handler)
    },
    emit(key, data) {
      if (all.has(key)) {
        all.get(key).forEach(handler => handler(data))
      }
    },
    off(key){
      if (all.has(key)) {
        all.delete(key)
      }
    }
  }
}

export default emit()